package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.provider.model.dto.life.alertToExcelDto;
import com.csun.cmny.provider.model.enums.device.DmcDeviceAlertTypeEnum;
import com.csun.cmny.provider.model.vo.device.DmcDeviceAlertLifeRoomVo;
import com.csun.cmny.provider.model.vo.life.DmcDeviceByRoom;
import com.csun.cmny.provider.model.vo.life.Status;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcDeviceAlertService;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.utils.JxlExcelUtils;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-09-28 14:11
 * @Description: the class api rest controller
 */
@Slf4j
@RestController
@RequestMapping(value = "/api")
@Api(value = "Web-ApiRestController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ApiRestController  {

    @Resource
    private DmcDeviceService dmcDeviceService;
    @Resource
    private DmcDeviceAlertService dmcDeviceAlertService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @GetMapping("/get_bg_image")
    public Map getBgImage() {

        Map<String, Object> map = Maps.newHashMap();
        map.put("status", Status.OK());
        map.put("bg_image", "/9j/4AAQSkZRgABAQEAZBkAAD/'//Z");
        map.put("errors", Lists.newArrayList());

        return map;
    }

    @RequestMapping(value = "/getMessage")
    public Wrapper getMessage(String code, Long groupId) {

        Object params[] = {"param-1"};
        String alertInfo = cmnyMessageSource.getMessage(code, params, groupId);

        return WrapMapper.ok(alertInfo);


    }



    @PostMapping("/sensor/send_frame_image")
    public Map sendFrameImage() {

        Map<String, Object> map = Maps.newHashMap();
        map.put("status", Status.OK());
        map.put("errors", Lists.newArrayList());

        return map;
    }

    @PostMapping("/sensor/send_bone_info")
    public Map sendBoneInfo() {

        Map<String, Object> map = Maps.newHashMap();
        map.put("status", Status.OK());
        map.put("errors", Lists.newArrayList());

        return map;
    }

    @RequestMapping("/down")
    public void down(HttpServletResponse response, alertToExcelDto alertToExcelDto) {

        List<Long> deviceIds = new ArrayList<>();
        List<DmcDeviceByRoom> dmcDeviceByRooms = dmcDeviceService.selectDeviceByRoomId(alertToExcelDto.getRoomId(),alertToExcelDto.getGroupId());
        for (DmcDeviceByRoom dmcDeviceByRoom : dmcDeviceByRooms) {
            deviceIds.add(dmcDeviceByRoom.getDeviceId());
        }

        if(PublicUtil.isNotEmpty(deviceIds)){
            List<DmcDeviceAlertLifeRoomVo> dmcDeviceAlertLifeRoomVos = dmcDeviceAlertService.queryDeviceAlertLifeRoomAndTime(deviceIds, alertToExcelDto.getGroupId(),alertToExcelDto.getStartTime(),alertToExcelDto.getEndTime());
            for (DmcDeviceAlertLifeRoomVo dmcDeviceAlertLifeRoomVo : dmcDeviceAlertLifeRoomVos) {
                if (dmcDeviceAlertLifeRoomVo.getIsHandle()==0){
                    dmcDeviceAlertLifeRoomVo.setUpdateTime("");
                }
                if(PublicUtil.isNotEmpty(dmcDeviceAlertLifeRoomVo.getCreatedTime())){
                    dmcDeviceAlertLifeRoomVo.setCreatedTime(dmcDeviceAlertLifeRoomVo.getCreatedTime().substring(0,dmcDeviceAlertLifeRoomVo.getCreatedTime().length()-2));
                }

                if ((PublicUtil.isNotEmpty(dmcDeviceAlertLifeRoomVo.getUpdateTime()))){
                    dmcDeviceAlertLifeRoomVo.setUpdateTime(dmcDeviceAlertLifeRoomVo.getUpdateTime().substring(0,dmcDeviceAlertLifeRoomVo.getUpdateTime().length()-2));
                }
                if(PublicUtil.isNotEmpty(dmcDeviceAlertLifeRoomVo.getAlertType())){
                    dmcDeviceAlertLifeRoomVo.setAlertTypeString(DmcDeviceAlertTypeEnum.getValue(dmcDeviceAlertLifeRoomVo.getAlertType()));
                    String typeString="";
                    switch (dmcDeviceAlertLifeRoomVo.getAlertType()){
                        case 14:
                            typeString="トイレ入り注意";
                            break;
                        case 15:
                            typeString="トイレ入り警告";
                            break;
                        case 16:
                            typeString="トイレ入り異常";
                            break;
                        case 12:
                            typeString="室温異常";
                            break;
                        case 17:
                            typeString="呼吸・心拍数異常";
                            break;
                        case 18:
                            typeString="呼吸・心拍数異常";
                            break;
                        case 11:
                            typeString="離床中";
                            break;
                        case 13:
                            typeString="転倒発生";
                            break;
                        case 20:
                            typeString="夜間巡回異常";
                            break;
                        case 22:
                            typeString="トイレセンサーOff Line";
                            break;
                        case 24:
                            typeString="温湿度センサーOff Line";
                            break;
                        case 23:
                            typeString="ゲートウェイOff Line";
                            break;
                        case 21:
                            typeString="カメラOff Line";
                            break;
                        case 10:
                            typeString="生体センサーOff Line";
                            break;
                    }
                    if(typeString!=""){
                        dmcDeviceAlertLifeRoomVo.setAlertTypeString(typeString);
                    }
                }
            }


            // String filename = "报警信息表";
            String filename = cmnyMessageSource.getMessage("alert.execl.file.name", null, alertToExcelDto.getGroupId());
            String titleName = cmnyMessageSource.getMessage("alert.execl.title.name", null, alertToExcelDto.getGroupId());
            String sheetName = cmnyMessageSource.getMessage("alert.execl.sheet.name", null, alertToExcelDto.getGroupId());
            List<String> columns = Lists.newArrayList();
            columns.add(cmnyMessageSource.getMessage("alert.execl.alert.id", null, alertToExcelDto.getGroupId()));
            columns.add(cmnyMessageSource.getMessage("alert.execl.alert.type", null, alertToExcelDto.getGroupId()));
            columns.add(cmnyMessageSource.getMessage("alert.execl.created.time", null, alertToExcelDto.getGroupId()));
            columns.add(cmnyMessageSource.getMessage("alert.execl.lastOperator.name", null, alertToExcelDto.getGroupId()));
            columns.add(cmnyMessageSource.getMessage("alert.execl.update.name", null, alertToExcelDto.getGroupId()));


            try {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("Content-Disposition",
                        "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");
                //  JxlExcelUtils.fcReportToExcel(response, dmcDeviceAlertLifeRoomVos, sheetName);



                JxlExcelUtils.test(response, dmcDeviceAlertLifeRoomVos, titleName, sheetName, columns);
            } catch (Exception e) {
                e.printStackTrace();
            }



        }

    }

}
